﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace engine
{
    /// <summary>
    /// 配置文件还没做，数据提取还没做
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Test6();
        }

        static void Test1()
        {
            // 读取word，构造分析树

            //TextRangeNode textRange = new TextRangeNode();
            //textRange.Text = "123abc";

            //ParagraphNode paragraph = new ParagraphNode();
            //paragraph.Children.Add(textRange);

            //BodyNode bodyNode = new BodyNode();
            //bodyNode.Children.Add(paragraph);

            //CellNode cellNode = new CellNode();
            //cellNode.Children.Add(bodyNode);

            //RowNode rowNode = new RowNode();
            //rowNode.Children.Add(cellNode);

            //TableNode tableNode = new TableNode();
            //tableNode.ColumnCount = 1;
            //tableNode.AddRow(rowNode);

            //// 分析树渲染为通用结构
            //var result = tableNode.RenderValue(null);

            //// 通用结构树转为文档树并输出文档
            //IRender render = new Render();
            //render.RenderToFile(result[0], "./test.docx");
        }

        static void Test2()
        {
            //Engine engine = new Engine();
            //// 数据提取
            //NodeBase nodeBase1 = engine.ParseTemplate("./test.docx", "");
            //Dictionary<string, object> data = engine.MatchFile(nodeBase1, "./test2.docx", new GlobalConfig());
            //var data2 = new Dictionary<string, DataItem>();
            //foreach(var item in data)
            //{
            //    data2.Add(item.Key, new DataItem(item.Value, null));
            //}
            //// 模板渲染
            //NodeBase nodeBase2 = engine.ParseTemplate("./test.docx", "");
            //engine.RenderTemplate(nodeBase2, data2, "./test2.docx");
        }

        static void Test3()
        {
            ITemplateFunc templateFunc = new TemplateFunc(FuncType.Render);
            UnitType? unitType = templateFunc.GetFuncType("Row");
            /// 接下来考虑函数的参数应该怎么处理，不同类型啥的
        }

        /// <summary>
        /// 测试标记解析是否可用
        /// </summary>
        static void Test4()
        {
            // 构造分析树
            //TextRangeNode textRange = new TextRangeNode(null);
            //textRange.Text = @"$row(@list@item@index)表格名字是：$table-name,当前的会员名字是：$item.name\.第一行的是$list[0].name";
            //textRange.CharactorFormat = new CharactorFormat
            //{
            //    FontSize = 5
            //};

            //ParagraphNode paragraph = new ParagraphNode(null);
            //paragraph.AddChild(textRange);

            //CellNode cellNode = new CellNode(null);
            //cellNode.AddChild(paragraph);

            //RowNode rowNode = new RowNode(null);
            //rowNode.AddChild(cellNode);

            //TableNode tableNode = new TableNode(null);
            //tableNode.ColumnCount = 1;
            //tableNode.AddRow(rowNode);

            //// 构造配置
            //TemplateConfig config = new TemplateConfig("", null);
            //TypeItem personType = new TypeItem {
            //    Name = "person",
            //    Props = new Dictionary<string, VarItem> { { "name", new StringVarItem() } }
            //};
            //ArrayVarItem personConfig = new ArrayVarItem(personType)
            //{
            //    Name = "list",
            //};
            //config.Vars.Add("list", personConfig);
            //StringVarItem tableConfig = new StringVarItem
            //{
            //    Name = "table-name",
            //    MaxLength = 2
            //};
            //config.Vars.Add("table-name", tableConfig);

            //// 开始解析标记
            //GlobalConfig globalConfig = new GlobalConfig();
            //globalConfig.TemplateConfig = config;
            //globalConfig.RootFuncNode.SetTemplateFunc(new TemplateFunc(FuncType.Render));
            //globalConfig.RootFuncNode.SetDatas(new Dictionary<string, DataItem>
            //{
            //    { "list", new DataItem(new List<object>{
            //        new Dictionary<string, object> { { "name", "张三" } },
            //        new Dictionary<string, object> { { "name", "李四" } }
            //    }, personConfig) } ,
            //    { "table-name", new DataItem("table1", tableConfig) }
            //});

            //MarkerParser parser = new MarkerParser(paragraph, globalConfig);
            //parser.ShouldThrowError = true;
            //parser.Parse();

            //int length = paragraph.Children.Count;
            //List<Unit> units = rowNode.RenderValue(null);
        }

        static void Test5()
        {
            // 构造分析树
            //TextRangeNode textRange = new TextRangeNode(null);
            //textRange.Text = @"$row(@list@item)表格名字是：$table-name,当前的会员名字是：$(item.name)\.第一行的是$list[0].name";
            //textRange.CharactorFormat = new CharactorFormat
            //{
            //    FontSize = 5
            //};

            //ParagraphNode paragraph = new ParagraphNode(null);
            //paragraph.AddChild(textRange);

            //CellNode cellNode = new CellNode(null);
            //cellNode.AddChild(paragraph);

            //RowNode rowNode = new RowNode(null);
            //rowNode.AddChild(cellNode);

            //TableNode tableNode = new TableNode(null);
            //tableNode.ColumnCount = 1;
            //tableNode.AddRow(rowNode);

            //// 构造配置
            //TemplateConfig config = new TemplateConfig("test-source-template.json", null);
            //config.Vars.Add("list", new ArrayVarItem(new TypeItem
            //{
            //    Name = "person",
            //    Props = new Dictionary<string, VarItem> { { "name", new StringVarItem() } }
            //})
            //{
            //    Name = "list",
            //});
            //config.Vars.Add("table-name", new StringVarItem
            //{
            //    Name = "table-name"
            //});

            //// 构造通过结构树
            //List<Unit> units = CreateTableUnit();

            //// 开始解析标记
            //GlobalConfig globalConfig = new GlobalConfig();
            //globalConfig.RootFuncNode.SetTemplateFunc(new TemplateFunc(FuncType.Match));
            //globalConfig.TemplateConfig = config;
            
            //MarkerParser parser = new MarkerParser(paragraph, globalConfig);
            //parser.ShouldThrowError = true;
            //parser.Parse();

            //tableNode.MatchData(units, globalConfig.RootFuncNode, null);

            //Dictionary<string, DataItem> data = globalConfig.RootFuncNode.GetDatasWithChildren(globalConfig.TemplateConfig);

        }

        static void Test6()
        {
            Engine engine = new Engine(new EngineRuntime());
            engine.SetRenderTemplateFunc(new TijianTemplateFunc());
            Dictionary<string, DataItem> data = engine.MatchFile("test-source-template.docx", "test-source-template.json", "test-source.docx");
            engine.RenderTemplate("test-target-template.docx", "test-target-template.json", data, "test-target.docx");
        }

        static List<Unit> CreateTableUnit()
        {
            TextRangeUnit textRange = new TextRangeUnit();
            textRange.Text = @"表格名字是：测试表格1,当前的会员名字是：张三.第一行的是李四";
            textRange.CharactorFormat = new CharactorFormat
            {
                FontSize = 5
            };

            ParagraphUnit paragraph = new ParagraphUnit();
            paragraph.AddChild(textRange);

            CellUnit cellNode = new CellUnit();
            cellNode.AddChild(paragraph);

            RowUnit rowNode = new RowUnit();
            rowNode.AddChild(cellNode);

            TableUnit tableNode = new TableUnit();
            tableNode.ColumnCount = 1;
            tableNode.AddChild(rowNode);

            return new List<Unit>
            {
                tableNode
            };
        }
    }
}
